{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e81d0858-6df9-44a4-950d-9e7304c34a3a",
   "metadata": {},
   "source": [
    "# Webgui \n",
    "\n",
    "Webgui is a jupyter-widget for visualizing mesh data, available from [project page](https://github.com/CERBSim/webgui_jupyter_widgets). It can be easily installed using \n",
    "\n",
    "    pip install webgui_jupyter_widgets\n",
    "\n",
    "\n",
    "Webgui contains a rendering area, and a simple user interface (which can be opened by clicking on the black rectangle showing 'Open Controls'). \n",
    "\n",
    "You can rotate, move and zoom the object in the rendering area, and click to query properties."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e8e5419-c665-4a5b-92f2-b554047d2ae3",
   "metadata": {},
   "source": [
    "## Drawing geometry, meshes and fields:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f08da2e5-5488-4164-9e0b-8e9b18687956",
   "metadata": {},
   "outputs": [],
   "source": [
    "from netgen.occ import *\n",
    "from ngsolve import * \n",
    "from ngsolve.webgui import Draw"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81b12da6-6dcd-474f-9e8c-3466d72312b2",
   "metadata": {},
   "source": [
    "Drawing on OCC-shape:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5950da73-663e-4ea9-8e79-61efcb75e18d",
   "metadata": {},
   "outputs": [],
   "source": [
    "shape = Box( (0,0,0), (5,3,2) )\n",
    "shape.faces.Max(X).name=\"right\"\n",
    "Draw (shape);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a39681d8-2cb6-4d40-aea5-e9b050b2257e",
   "metadata": {},
   "source": [
    "Drawing an NGSolve-mesh:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a635a265-7471-4026-996e-ffc7ac659b0c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "mesh = Mesh( OCCGeometry(shape).GenerateMesh(maxh=0.5))\n",
    "Draw (mesh); "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4905e634-8e95-41ad-9f6a-4ffcddf21e80",
   "metadata": {},
   "source": [
    "Drawing a scalar function on the mesh:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af1ab686-6169-4eb3-8824-a2afd8c48fa4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "func = x*y*z\n",
    "Draw (func, mesh);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32a79217-2df8-4758-a38d-5de5fb3a24a3",
   "metadata": {},
   "source": [
    "## Setting min-max values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "714a289b-7ffb-42b5-a763-681e827ee5be",
   "metadata": {},
   "outputs": [],
   "source": [
    "Draw (x*y*z, mesh, min=5, max=10);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8af8d6f-5854-42e1-9b0f-aa3ba00d9de5",
   "metadata": {},
   "source": [
    "## Deformation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0356b63e-3749-43c6-9146-0679d915a039",
   "metadata": {},
   "outputs": [],
   "source": [
    "deform = CF( (-0.05*z*x,0,0.05*x*x) )\n",
    "Draw (mesh, deformation=deform);\n",
    "\n",
    "print (\"use deformation slider:\")\n",
    "Draw (deform, mesh);  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93bd57ae-041e-46a3-b78f-f21c848b51fd",
   "metadata": {},
   "source": [
    "## Clipping planes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fba7465-0cc1-4c83-91b6-b0f4e127dd13",
   "metadata": {},
   "outputs": [],
   "source": [
    "clipping = { \"function\" : True,  \"pnt\" : (2.5,1.5,1), \"vec\" : (0,1,-1) } \n",
    "Draw (func, mesh, clipping=clipping);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12a06603-0f67-48d8-b835-9baae155740d",
   "metadata": {},
   "source": [
    "## Vector plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "809e4104-7f73-4840-ab05-4c7be1ec6961",
   "metadata": {},
   "outputs": [],
   "source": [
    "rotfunc = CF( (y-0.5, 0.5-x) )\n",
    "usmesh = Mesh(unit_square.GenerateMesh(maxh=0.1))\n",
    "Draw (rotfunc, usmesh, max=0.7,  vectors={\"grid_size\" : 20, \"offset\" : 0.5 } );"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddc62a8b-4b1a-4062-b47d-b6e4f5492b66",
   "metadata": {},
   "source": [
    "## Setting gui parameters\n",
    "\n",
    "all parameters from the gui can be set by their displayed names, for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2399836c-debf-44db-94b8-7ff62050437e",
   "metadata": {},
   "outputs": [],
   "source": [
    "Draw (func, mesh, settings = { \"subdivision\" : 1, \"Colormap\" : { \"ncolors\" : 3 } });"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9a5807a-45df-4594-9eb0-7c595eea2574",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
